package com.sxj.corejava.code07_面向对象.bms;

import java.util.Scanner;

/**
 * @author 石小俊
 * 2024年07月17日 13:53
 * 用户登录成功之后进入的主菜单
 * 该类中没有main方法,不能直接运行
 */
public class AccountMenu {

    private Scanner sc = new Scanner(System.in);
    private Account account;

    public AccountMenu(Account account) {
        this.account = account;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public void showMenu() {
        // 当用户成功登录进入银行系统之后显示的主菜单页面
        // 主菜单主要提供以下功能:1.存款 2.取款 3.查询余额 4.转账 5.贷款 6.还款 7.查询贷款金额 8.修改密码 9.退出登录
        // 通过控制台输入的方式实现对应功能
        System.out.print("请选择服务(1.存款 2.取款 3.查询余额 4.转账 5.贷款 6.还贷 7.查询贷款金额 8.修改密码 9.退出登录):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                deposit();
                break;
            case 2:
                withdraw();
                break;
            case 3:
                queryBalance();
                break;
            case 4:
                transfer();
                break;
            case 5:
                providerLoan();
                break;
            case 6:
                payLoan();
                break;
            case 7:
                queryLoan();
                break;
            case 8:
                modifyPassword();
                break;
            case 9:
                logout();
                break;
            default:
                System.out.println("错误:您输入的选项有误,请重新输入");
                showMenu();
        }
    }

    public void deposit() {
        System.out.println("*******************开始存款*******************");
        System.out.print("请输入存款金额:");
        double money = sc.nextDouble();
        account.deposit(money);
        System.out.println("提示:恭喜您,成功存款" + money + "元");
        showMenu();
    }

    public void withdraw() {
        System.out.println("*******************开始取款*******************");
        System.out.print("请输入取款金额:");
        double money = sc.nextDouble();
        boolean flag = account.withdrawMoney(money);
        if (flag) {
            System.out.println("提示:恭喜您,成功取款" + money + "元!");
        } else {
            System.out.println("错误:对不起,您的账户余额不足,取款失败!");
        }
        showMenu();

    }

    public void queryBalance() {
        System.out.println("*******************查询余额*******************");
        System.out.println("提示:您当前账户余额为:" + account.getBalance() + "元");
        showMenu();
    }

    public void transfer() {
        System.out.println("*******************开始转账*******************");
        System.out.print("请输入目标账户卡号:");
        int id = sc.nextInt();

        // 判断对应目标账户是否存在
        Account targetAccount = Bank.findById(id);
        // 如果对应账户不存在
        if (targetAccount == null) {
            System.out.println("错误:您输入的卡号有误!");
        } else {
            // 如果目标账户存在,则执行转账功能
            System.out.print("请输入转账金额:");
            double money = sc.nextDouble();
            // 判断当前账户是否是储蓄账户
            if (account instanceof SavingAccount) {
                // 如果是储蓄账户,则当前账户余额不能低于转账的金额
                if (account.getBalance() >= money) {
                    // 转账之后发生两个变化
                    // 1.当前账户余额减少
                    account.setBalance(account.getBalance() - money);
                    // 2.目标账户余额增加
                    targetAccount.setBalance(targetAccount.getBalance() + money);
                    System.out.println("提示:恭喜您,转账成功!");
                } else {
                    System.out.println("错误:对不起,您当前账户余额不足,转账失败!");
                }
            } else {
                // 如果不是储蓄账户,则说明是信用账户
                CreditAccount creditAccount = (CreditAccount) account;
                // 因此,此时账户余额与信用额度之和不能低于转账的金额
                if (creditAccount.getBalance() + creditAccount.getLimit() >= money) {
                    account.setBalance(account.getBalance() - money);
                    targetAccount.setBalance(targetAccount.getBalance() + money);
                    System.out.println("提示:恭喜您,转账成功!");
                } else {
                    System.out.println("错误:对不起,您当前账户余额不足,转账失败!");
                }
            }
        }
        showMenu();
    }

    public void providerLoan() {
        System.out.println("*******************开始贷款*******************");
        // 由于只有贷款的储蓄账户与贷款的信用账户可以实现贷款相关功能
        // 因此,必须对当前账户类型进行判断
        //Loanable account1 = new LoanSavingAccount();
        //Loanable account2 = new LoanCreditAccount();
        // 当当前账户可以向下转型为Loanable接口,则说明当前账户一定是实现了Loanable接口的账户
        // 只有贷款相关账户才会实现对应接口
        if (account instanceof Loanable) {
            System.out.print("请输入贷款金额:");
            double money = sc.nextDouble();
            // 贷款相关方法只有接口以及接口对应的实现类可以调用
            // 因此,先向下转型
            Loanable loanable = (Loanable) account;
            loanable.providerLoan(money);
            System.out.println("提示:恭喜您,成功贷款" + money + "元!");
        } else {
            System.out.println("错误:对不起,您的账户不是贷款账户,无法进行贷款!");
        }
        showMenu();
    }

    public void payLoan() {
        System.out.println("*******************开始还贷*******************");
        if (account instanceof Loanable) {
            Loanable loanable = (Loanable) account;
            System.out.print("请输入还贷金额:");
            double money = sc.nextDouble();
            boolean flag = loanable.payLoan(money);
            if (flag) {
                System.out.println("提示:恭喜您,成功还贷" + money + "元,剩余贷款总额为:" + loanable.query() + "元!");
            } else {
                System.out.println("错误:对不起,还贷失败,当前账户余额不足,当前账户余额为:" + account.getBalance() + "元");
            }
        } else {
            System.out.println("错误:对不起,您的账户不是贷款账户,无法进行贷款!");
        }
        showMenu();
    }

    public void queryLoan() {
        System.out.println("*******************查询贷款金额*******************");
        if (account instanceof Loanable) {
            Loanable loanable = (Loanable) account;
            System.out.println("提示:您当前贷款总额为:" + loanable.query());
        } else {
            System.out.println("错误:对不起,您的账户不是贷款账户,无法进行贷款!");
        }
        showMenu();
    }

    public void modifyPassword() {
        System.out.println("*******************修改密码*******************");
        System.out.print("请输入原密码:");
        String oldPassword = sc.next();
        // 判断原密码是否正确
        if (oldPassword.equals(account.getPassword())) {
            int i = 0;
            while (true) {
                System.out.print("请输入新密码:");
                String newPassword = sc.next();
                System.out.print("请再次输入新密码:");
                String newPassword2 = sc.next();
                if (newPassword.equals(newPassword2)) {
                    // 如果两次密码输入一致
                    // 设置设置密码
                    account.setPassword(newPassword);
                    // 并退出循环
                    break;
                }
                System.out.println("错误:两次输入的密码不一致,请重新输入");
                i++;
                if (i == 3) {
                    System.out.println("错误:您输入的错误次数过多,已退出!");
                    showMenu();
                    return;
                }
            }
            new Start().showMenu();
        } else {
            System.out.println("错误:您输入的原密码不正确!");
            showMenu();
        }

    }

    public void logout() {
        System.out.println("提示:注销成功!");
        new Start().showMenu();
    }


}
